////////////////////////////
/////Lightsber SWEP /////
/////////by shotgunguy49/////////
////////////////////////////

if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
end

if ( CLIENT ) then

	SWEP.PrintName			= "Lightsaber"			
	SWEP.Author				= "Shotgunguy49"
	SWEP.Category		= "Star Wars"
	SWEP.Slot				= 1
	SWEP.SlotPos			= 3
	SWEP.ViewModelFOV		= 60
	SWEP.IconLetter			= "x"

end
-----------------------Main functions----------------------------
 
function SWEP:Think() -- Called every frame
end

sounds = {
	"deploy1",
	"deploy2",
	"hitwall1",
	"idle",
	"slash1", 
	"stab1",
	"stab2",
	"stab3"
	}

function SWEP:Initialize()
			self:SetWeaponHoldType("sword");
	for k, v in pairs ( sounds ) do
		util.PrecacheSound( "weapons/lightsaber/lightsaber_"..v..".wav" )
	end
end
 
 
function SWEP:PrimaryAttack()
local tr = self.Owner:GetEyeTrace()
	tr.endpos = self.Owner:GetPos() + (self.Owner:GetAimVector() * 150)
local length = (self.Owner:GetShootPos() - tr.Entity:GetPos()):Length()
	
	
	if length <= 125 then
		if tr.Entity:IsValid() and tr.Entity:IsNPC() or tr.Entity:IsPlayer() then	
			self.Weapon:EmitSound("weapons/lightsaber/lightsaber_hitflesh"..math.random( 1, 3 )..".wav")
			self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )
			util.BlastDamage(self.Owner, self.Owner, tr.HitPos, 4, math.random( 60, 100 ) )

		elseif !tr.HitWorld then
			self.Weapon:EmitSound("weapons/lightsaber/lightsaber_hit"..math.random( 1, 2 )..".wav")
			self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )
			util.BlastDamage(self.Owner, self.Owner, tr.HitPos, 4, math.random( 60, 100 ) )			
			
			local Pos1 = tr.HitPos + tr.HitNormal
			local Pos2 = tr.HitPos - tr.HitNormal
				util.Decal( "ManhackCut", Pos1, Pos2 )
		end
	else
		self.Weapon:EmitSound( "weapons/lightsaber/lightsaber_miss.wav" )
		self.Weapon:SendWeaponAnim( ACT_VM_MISSCENTER )
	end
	self.Weapon:SetNextPrimaryFire( CurTime() + 0.5 )
end
 
function SWEP:SecondaryAttack()
	return false
end

function SWEP:Reload()
	return false
end

function SWEP:Deploy()
	self.Weapon:EmitSound( "weapons/lightsaber/lightsaber_draw.wav" )
	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
	timer.Create( "IdleSound", 2.99, 0, self.Weapon.EmitSound, self.Weapon, "weapons/lightsaber/lightsaber_idle.wav" )
end

function SWEP:Holster()
	timer.Destroy( "IdleSound" )
	self.Weapon:StopSound( "weapons/lightsaber/lightsaber_idle.wav" )
	return true
end
-------------------------------------------------------------------

------------General Swep Info---------------
SWEP.Author   = "shotgunguy49"
SWEP.Contact        = ""
SWEP.Purpose        = "Cut something... maybe some legs or arms"
SWEP.Instructions   = "Cut things up."
SWEP.Spawnable      = true
SWEP.AdminSpawnable  = true
-----------------------------------------------

------------Models---------------------------
SWEP.ViewModel      = "models/SGG/Starwars/weapons/v_luke_saber.mdl"
SWEP.WorldModel   = "models/SGG/Starwars/weapons/w_luke_saber.mdl"
-----------------------------------------------

-------------Primary Fire Attributes----------------------------------------
SWEP.Primary.Delay			= 0 	--In seconds
SWEP.Primary.Recoil			= 0		--Gun Kick
SWEP.Primary.Damage			= 100	--Damage per Bullet
SWEP.Primary.NumShots		= 0		--Number of shots per one fire
SWEP.Primary.Cone			= 20 	--Bullet Spread
SWEP.Primary.ClipSize		= -1	--Use "-1 if there are no clips"
SWEP.Primary.DefaultClip	= -1	-- Number of shots in next clip
SWEP.Primary.Automatic   	= true	--Pistol fire (false) or SMG fire (true)
SWEP.Primary.Ammo         	= "none"	--Ammo Type
-------------End Primary Fire Attributes------------------------------------
 
-------------Secondary Fire Attributes-------------------------------------
SWEP.Secondary.Delay		= 0
SWEP.Secondary.Recoil		= 0
SWEP.Secondary.Damage		= 0
SWEP.Secondary.NumShots		= 0
SWEP.Secondary.Cone			= 0
SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic   	= false
SWEP.Secondary.Ammo         = "none"
-------------End Secondary Fire Attributes--------------------------------

	local ActIndex = {}
	ActIndex["pistol"] 		= ACT_HL2MP_IDLE_PISTOL
	ActIndex["smg"] 			= ACT_HL2MP_IDLE_SMG1
	ActIndex["grenade"] 		= ACT_HL2MP_IDLE_GRENADE
	ActIndex["ar2"] 			= ACT_HL2MP_IDLE_AR2
	ActIndex["shotgun"] 		= ACT_HL2MP_IDLE_SHOTGUN
	ActIndex["rpg"]	 		= ACT_HL2MP_IDLE_RPG
	ActIndex["physgun"] 		= ACT_HL2MP_IDLE_PHYSGUN
	ActIndex["crossbow"] 		= ACT_HL2MP_IDLE_CROSSBOW
	ActIndex["melee"] 		= ACT_HL2MP_IDLE_MELEE
	ActIndex["slam"] 			= ACT_HL2MP_IDLE_SLAM
	ActIndex["normal"]		= ACT_HL2MP_IDLE
	ActIndex["knife"]			= ACT_HL2MP_IDLE_KNIFE
	ActIndex["sword"]			= ACT_HL2MP_IDLE_MELEE2
	ActIndex["passive"]		= ACT_HL2MP_IDLE_PASSIVE
	ActIndex["fist"]			= ACT_HL2MP_IDLE_FIST

function SWEP:SetWeaponHoldType(t)
	local index = ActIndex[t]
	if (index == nil) then
		Msg("SWEP:SetWeaponHoldType - ActIndex[ \""..t.."\" ] isn't set!\n")
		return
	end
	self.ActivityTranslate = {}
	self.ActivityTranslate [ACT_HL2MP_IDLE] 					= index
	self.ActivityTranslate [ACT_HL2MP_WALK] 					= index + 1
	self.ActivityTranslate [ACT_HL2MP_RUN] 					= index + 2
	self.ActivityTranslate [ACT_HL2MP_IDLE_CROUCH] 				= index + 3
	self.ActivityTranslate [ACT_HL2MP_WALK_CROUCH] 				= index + 4
	self.ActivityTranslate [ACT_HL2MP_GESTURE_RANGE_ATTACK] 		= index + 5
	self.ActivityTranslate [ACT_HL2MP_GESTURE_RELOAD] 			= index + 6
	self.ActivityTranslate [ACT_HL2MP_JUMP] 					= index + 7
	self.ActivityTranslate [ACT_RANGE_ATTACK1] 				= index + 8
end

function SWEP:TranslateActivity(act)
	if (self.Owner:IsNPC()) then
		if (self.ActivityTranslateAI[act]) then
			return self.ActivityTranslateAI[act]
		end
		return -1
	end
	if (self.ActivityTranslate[act] != nil) then
		return self.ActivityTranslate[act]
	end
	return -1
end